package org.example.sever;

import lombok.SneakyThrows;
import org.example.POJO.online.MyMessage;
import org.example.Utils.SocketUtil;
import java.net.Socket;

public class ClientThread extends Thread {
    private Socket socket;
    private ResponseListener listener;

    //线程开关
    private boolean shutdown;

    public void setShutdown(boolean shutdown) {
        this.shutdown = shutdown;
    }

    public ClientThread(Socket socket, ResponseListener listener) {
        this.socket = socket;
        this.listener = listener;
    }

    public interface ResponseListener {
        void Success(MyMessage response);
    }

    @SneakyThrows
    @Override
    public void run() {
        while (!shutdown) {
            MyMessage receive = SocketUtil.receive(socket);
            if (receive instanceof MyMessage) {
                if (listener != null) {
                    listener.Success(receive);
                }
            }
        }
    }
}
